package com.chen.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

public class Test {


	/**
	 * 
	* @Title: main 
	* @Description: 4种json技术解析对比 json-lib,jackson,Gson,FastJson
	* http://blog.csdn.net/pangliang_csdn/article/details/51595375 
	* @param @param args    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public static void main(String[] args) {
		Student student = new Student();
		student.setId(1);
		student.setName("chen");
		
		Course course = new Course();
		course.setId(2);
		course.setName("course 2");
		student.setCourse(course);
		
		//jackSon
		ObjectMapper mapper = new ObjectMapper ( );
		try {
			String jsonStr = mapper.writeValueAsString(student);
			System.out.println(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//Gson
		//bean->json
		Gson gson = new Gson();
		String json = gson.toJson(student);
		System.out.println(json);
		
		//json->bean
		String jsonStr = "{\"id\":1,\"name\":\"chen\",\"course\":{\"id\":2,\"name\":\"course 2\"}}";
		Student student2 = gson.fromJson(jsonStr, Student.class);
		System.out.println(student2.getName() + "," + student2.getCourse().getName());
	}

}
